<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import dayjs from 'dayjs';
  import { useRouter } from 'vue-router';
  import useTable from '@/hooks/table';
  import { CourseApproveItem, CourseInviteSearchParams } from '@/types/course';
  import { getApproveList } from '@/api/course';
  import { CourseStatus, CourseStatusColors } from '@/config/course';
  import CourseCard from '@/components/course-card/index.vue';
  import SearchForm from './search-form.vue';

  const { pager, loading, total, list } = useTable<CourseApproveItem>();

  const searchParams = ref<CourseInviteSearchParams>({});

  const router = useRouter();

  const handleViewDetail = (inviteItem: CourseApproveItem) => {
    const routeInfo = router.resolve({
      name: 'CourseInviteDetailApp',
      query: {
        id: inviteItem.id,
      },
    });
    window.open(routeInfo.href);
  };

  const getData = async () => {
    loading.value = true;
    try {
      const res = await getApproveList({
        ...pager.value,
        ...searchParams.value,
      });
      total.value = res?.data?.data?.total || 0;
      list.value = res?.data?.data?.list || [];
    } finally {
      loading.value = false;
    }
  };

  const handleSearch = (params: CourseInviteSearchParams) => {
    pager.value.pageIndex = 1;
    searchParams.value = params;
    getData();
  };

  const handlePageIndexChanged = (pageIndex: number) => {
    pager.value.pageIndex = pageIndex;
    getData();
  };

  onMounted(() => {
    getData();
  });
</script>

<template>
  <div style="padding: 20px">
    <a-row :gutter="[20, 20]">
      <a-col>
        <a-card title="球场审批" :bordered="false">
          <SearchForm @submit="handleSearch" />
        </a-card>
      </a-col>

      <a-col>
        <a-card :bordered="false">
          <a-table
            :bordered="{ cell: true }"
            :data="list"
            :loading="loading"
            :pagination="{
              total,
              current: pager.pageIndex,
              pageSize: pager.pageSize,
              showTotal: true,
              showJumper: true,
            }"
            @page-change="handlePageIndexChanged"
          >
            <template #columns>
              <a-table-column title="球场" data-index="name">
                <template #cell="{ record }">
                  <CourseCard :course-info="record" />
                </template>
              </a-table-column>
              <a-table-column
                title="手机号"
                data-index="phone"
              ></a-table-column>
              <a-table-column title="联系人" data-index="contactName" />
              <a-table-column title="时间" data-index="gmtCreate" :width="200">
                <template #cell="{ record }">
                  {{ dayjs(record.gmtCreate).format('YYYY/MM/DD HH:mm:ss') }}
                </template>
              </a-table-column>
              <a-table-column
                title="状态"
                align="center"
                data-index="status"
                :width="120"
              >
                <template #cell="{ record }">
                  <a-tag :color="CourseStatusColors[record.status]">
                    {{ CourseStatus[record.status] }}
                  </a-tag>
                </template>
              </a-table-column>
              <a-table-column title="操作" align="center" :width="100">
                <template #cell="{ record }">
                  <a-space>
                    <a-link @click="handleViewDetail(record)"> 查看 </a-link>
                  </a-space>
                </template>
              </a-table-column>
            </template>
          </a-table>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
